﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GSite.Model.UnitOfWork;
using GSite.Model.Entities;

namespace GSite.Model.Repositories
{
    public class UserRepository : RepositoryBase, IUserRepository
    {
        public UserRepository(IUnitOfWork unitOfWork)
        {
            UnitOfWork = unitOfWork;
        }

        public User GetUserById(Guid id)
        {
            return DataContext.Users.FirstOrDefault(u => u.Id == id);
        }

        public IEnumerable<User> GetAllUsers()
        {
            return DataContext.Users;
        }

        public User CreateUser()
        {
            User u = new User();
            u.Id = Guid.NewGuid();
            DataContext.AddToUsers(u);
            u.IsActive = true;
            return u;
        }

        public User GetUserByUsername(string userName)
        {
            return DataContext.Users.FirstOrDefault(u => u.Username == userName);
        }

        public bool HasUser(string userName, string password)
        {
            return DataContext.Users.Any(u => u.Username == userName && u.Password == password);
        }

        public void DeleteUser(Guid id)
        {
            User user = DataContext.Users.Single(u => u.Id == id);
            DataContext.Users.DeleteObject(user); 
        }

        public User GetUserByPersonOrEntityIdentifier(string identificationNumber)
        {
            return DataContext.Users.FirstOrDefault(u => u.PersonOrEntityIdentifier == identificationNumber);
        }
    }
}
